package test.serverframe.armc.server.manager.dao.mapper;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.jdbc.SQL;
import test.serverframe.armc.server.manager.domain.TaskInvoke;

import java.util.List;
import java.util.Map;

public class TaskInvokeSqlProvider {

    /**
     * @Descripttion 添加验证
     * @Author LSY
     * @Date 2018/10/10 13:25
     * @Param [record 任务调度实体]
     * @Return
     **/
    public String insertSelective(TaskInvoke record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("tb_ssgp_task_invoke");

        if (record.getTaskInvokeId() != null) {
            sql.VALUES("task_invoke_id", "#{taskInvokeId,jdbcType=VARCHAR}");
        }

        if (record.getClassName() != null) {
            sql.VALUES("class_name", "#{className,jdbcType=VARCHAR}");
        }

        if (record.getMethodName() != null) {
            sql.VALUES("method_name", "#{methodName,jdbcType=VARCHAR}");
        }

        if (record.getInvokeName() != null) {
            sql.VALUES("invoke_name", "#{invokeName,jdbcType=VARCHAR}");
        }

        if (record.getRemark() != null) {
            sql.VALUES("remark", "#{remark,jdbcType=VARCHAR}");
        }

        if (record.getStatus() != null) {
            sql.VALUES("status", "#{status,jdbcType=BIT}");
        }

        if (record.getUsed() != null) {
            sql.VALUES("is_used", "#{isUsed,jdbcType=BIT}");
        }
        if (record.getUpdataTime() != null) {
            sql.VALUES("updata_time" , "#{updataTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getFailedStrategy() != null) {
        	sql.VALUES("failed_strategy", "#{failedStrategy,jdbcType=VARCHAR}");
        }
        
        if (record.getUsed() != null) {
            sql.VALUES("is_concurrent", "#{isConcurrent,jdbcType=BIT}");
        }
        
        if (record.getExecutePlanData() != null) {
            sql.VALUES("execute_plan_data", "#{executePlanData,jdbcType=VARCHAR}");
        }
        
        if (record.getNodeArr() != null) {
            sql.VALUES("node_arr", "#{nodeArr,jdbcType=VARCHAR}");
        }
        
        return sql.toString();
    }

    /**
     * @Descripttion 验证修改
     * @Author LSY
     * @Date 2018/10/10 13:24
     * @Param [record 任务调度实体]
     * @Return
     **/
    public String updateByPrimaryKeySelective(TaskInvoke record) {
        SQL sql = new SQL();
        sql.UPDATE("tb_ssgp_task_invoke");

        if (record.getInvokeName() != null) {
            sql.SET("invoke_name = #{invokeName,jdbcType=VARCHAR}");
        }

        if (record.getRemark() != null) {
            sql.SET("remark = #{remark,jdbcType=VARCHAR}");
        }
        
        if (record.getClassName() != null) {
            sql.SET("class_name = #{className,jdbcType=VARCHAR}");
        }

        if (record.getMethodName() != null) {
            sql.SET("method_name = #{methodName,jdbcType=VARCHAR}");
        }

        if (record.getStatus() != null) {
            sql.SET("status = #{status,jdbcType=BIT}");
        }

        if (record.getUsed() != null) {
            sql.SET("is_used = #{isUsed,jdbcType=BIT}");
        }
        if (record.getUpdataTime() != null) {
            sql.SET("updata_time = #{updataTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getFailedStrategy() != null) {
        	sql.SET("failed_strategy = #{failedStrategy,jdbcType=VARCHAR}");
        }

        if (record.isConcurrent() != null) {
            sql.SET("is_concurrent = #{isConcurrent,jdbcType=BIT}");
        }
        if (record.getExecutePlanData() != null) {
            sql.SET("execute_plan_data = #{executePlanData,jdbcType=VARCHAR}");
        }
        
        if (record.getNodeArr() != null) {
            sql.SET("node_arr = #{nodeArr,jdbcType=VARCHAR}");
        }
        
        sql.WHERE("task_invoke_id = #{taskInvokeId,jdbcType=VARCHAR}");

        return sql.toString();
    }

    /**
     * @Descripttion 分页条件查询
     * @Author LSY
     * @Date 2018/10/10 13:27
     * @Param [taskInvoke 任务调度实体]
     * @Return
     **/
    public String selectByPage(Map<String, Object> params) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT   " +
                " t.task_invoke_id,   " +
                " t.invoke_name,   " +
                " t.remark, " +
                " t.status, " +
                " t.updata_time,"+
                " t.execute_plan_data,"+
                " last.total ,   " +
                " record.STATUS AS last_status,   " +
                " record.start_time AS last_start_time,   " +
                " record.task_total AS last_task_total,   " +
                " record.task_success_total AS last_task_success_total,   " +
                " record.record_id AS last_invoke_record_id,   " +
//                " tb_record.task_group_id AS group_id," +
//                " tb_record.version AS ver" +
				" d.tag tag " +
                " FROM   " +
                " tb_ssgp_task_invoke t   " +
                " LEFT JOIN (   " +
                " SELECT   " +
                "  max( b1.record_id ) AS id,   " +
                "  b1.task_invoke_id,b2.total   " +
                " FROM   " +
                "  tb_ssgp_invoke_record b1   " +
                "  INNER JOIN (    " +
                "   " +
                " SELECT    " +
                " task_invoke_id, max( start_time ) AS last_time  " +
                ",COUNT(task_invoke_id) as total   " +
                "  FROM tb_ssgp_invoke_record GROUP BY task_invoke_id   " +
                "   " +
                ")b2 ON b1.task_invoke_id = b2.task_invoke_id    " +
                "  AND b1.start_time = b2.last_time    " +
                " GROUP BY   " +
                "  b1.task_invoke_id    " +
                " ) last ON t.task_invoke_id = last.task_invoke_id   " +
                " LEFT JOIN tb_ssgp_invoke_record record ON record.record_id = last.id   " +
//                " LEFT JOIN tb_ssgp_invoke_task_record tb_record ON record.record_id = tb_record.invoke_record_id "+
                " LEFT JOIN ( " +
                " 	SELECT link.invoke_id, group_concat(tag.tag) AS tag " +
                "	FROM tb_ssgp_invoke_enter_tag link LEFT JOIN tb_ssgp_tag tag " +
                "		ON link.tag_id = tag.id " +
                "	GROUP BY link.invoke_id "+ 
                " ) d ON t.task_invoke_id = d.invoke_id " +
                " WHERE   " +
                " 1 = 1    " +
                " AND t.is_used = 1");
        if (params.get("status")!=null){
        if ((Integer) params.get("status") == 3 || (Integer) params.get("status") == 2 || (Integer) params.get("status") == 1|| (Integer) params.get("status") == 0) {
            sql.append(" and record.status = " + (Integer) params.get("status"));
        }}
        Object name = params.get("invokeName");
        if (name != null && StringUtils.isNotBlank(name.toString())) {
            sql.append(" and t.invoke_name like '%" + name.toString() + "%'");
        }

        sql.append(" ORDER BY ");
        sql.append(" t.updata_time DESC");
        return sql.toString();
    }

    /**
     * @Descripttion 删除任务调度（修改任务调度是否可用）
     * @Author LSY
     * @Date 2018/10/11 10:02
     * @Param [taskInvokeIds 任务调度id集合]
     * @Return 修改任务调度是否可用sql
     **/
    public String deleteInvoke(List<String> taskInvokeIds) {
        SQL sql = new SQL();
        StringBuilder sb = new StringBuilder();
        sql.UPDATE("tb_ssgp_task_invoke");
        sql.SET("is_used = 0");

        for (String taskInvokeId : taskInvokeIds) {
            sb.append("or task_invoke_id = '");
            sb.append(taskInvokeId);
            sb.append("'");
        }

        sql.WHERE(sb.toString().replaceFirst("or", ""));
        return sql.toString();
    }

    public String selectSelective(TaskInvoke taskInvoke) {
        SQL sql = new SQL();
        StringBuilder sb = new StringBuilder();
        sql.SELECT("task_invoke_id");
        sql.SELECT("class_name");
        sql.SELECT("method_name");
        sql.SELECT("params");
        sql.SELECT("invoke_name");
        sql.SELECT("remark");
        sql.SELECT("start_time");
        sql.SELECT("end_time");
        sql.SELECT("status");
        sql.SELECT("is_used");
        sql.SELECT("updata_time");
        sql.SELECT("failed_strategy");
        sql.SELECT("is_concurrent");
        sql.SELECT("execute_plan_data");
        sql.FROM("tb_ssgp_task_invoke");
        sb.append("is_used=1");
        /*if (taskInvoke.get("taskInvokeId") != null) {
        	sb.append("and task_invoke_id =");
        	sb.append(taskInvoke.get("taskInvokeId"));
        }
        if (taskInvoke.get("invokeName") != null) {
        	sb.append("and invoke_name =");
        	sb.append(taskInvoke.get("invokeName"));
        }*/
        if (StringUtils.isNotEmpty(taskInvoke.getTaskInvokeId())) {
            sb.append(" and task_invoke_id = '");
            sb.append(taskInvoke.getTaskInvokeId());
            sb.append("'");
        }
        if (StringUtils.isNotEmpty(taskInvoke.getInvokeName())) {
            sb.append(" and invoke_name = '");
            sb.append(taskInvoke.getInvokeName());
            sb.append("'");
        }
        sql.WHERE(sb.toString());
        return sql.toString();
    }
}
